var app = getApp()
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
var tcity = require("../../../../utils/citys.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinces: [], //省份
    provincesdex: 0,
    citys: [], //城市
    citysdex: 0,
    needFix: false,
    bannerHeight: 0,
    tabdex: "",
    popshow: false,
    money: "",
    tablist: ["全部地区", "所属级别"],
    sortlists: [{
        items: "距离最近",
        checked: false
      },
      {
        items: "二甲医院",
        checked: false
      },
      {
        items: "三甲医院",
        checked: false
      },
      {
        items: "儿童医院",
        checked: false
      },
      {
        items: "骨科甲医院",
        checked: false
      },
    ],
    level_arr: [],
    level: "",
    hospitallm: [],
    page: 0,
    pagesize: 10,
    order: "",
    keyword: '',
    city: "",
    typs: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getallHospital('', '', '')
    that.getLevel();
    var leixing = options.leixing;
    var id = options.id;
    var money = options.money;
    if (money != '' && money != undefined) {
      that.setData({
        money: money,
      })
    }
    if (leixing != '' && leixing != undefined) {
      that.setData({
        leixing: leixing
      })
    }
    if (id != '' && id != undefined) {
      that.setData({
        id: id
      })
    }
    var pinyin = options.pinyin
    var typs = options.typs;
    var name = options.name;
    if (name != undefined) {
      that.setData({
        name: name
      })
    }
    if (typs != undefined) {
      that.setData({
        typs: typs
      })
    }
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.getHotcity();
    that.setData({

      pinyin: pinyin
    })
  },
  getHotcity: function () {
    var that = this;
    qqmapsdk = new QQMapWX({
      key: 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM' // 必填
    });
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标  
      success: function (res) {
        console.log(res);
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            console.log(addressRes);
            var addressed = addressRes.result.address_component.city
            
            app.util.request({
            'url':"entry/wxapp/zhuanjia.hot_address",
            success:function(res)
            {
              var citys = [];
              citys.push('全国')
              citys.push(addressed)
              for(var i=0;i<res.data.length;i++)
              {
                citys.push(res.data[i].name)
              }
              tcity.init(that);
              var cityData = that.data.cityData;
              const provinces = [];
              provinces.push("热门城市");
              for (let i = 0; i < cityData.length; i++) {
                provinces.push(cityData[i].name);
              }
              
              that.setData({
                provinces,
                citys,
                city_arr:citys
              })
            }
            })
          }
        })
      },

      fail: function () {
        // fail  
      },
      complete: function () {
        // complete  
      }
    })

  },
  loadCity: function (longitude, latitude) {
    var page = this

  },
  // 输入搜索
  getSearch: function (e) {
    var that = this;
    var keyword = e.detail.value;
    that.setData({
      keyword: keyword
    })
  },
  // 确认搜索
  search: function () {
    var that = this;
    var keyword = that.data.keyword;
    var order = that.data.order;
    var city = that.data.city;
    that.setData({
      page: 0,
      hospitallm: []
    })
    that.getallHospital(keyword, city, order)
  },
  // 获取级别
  getLevel: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.getLevel",
      success: function (res) {
        that.setData({
          level_arr: res.data
        })
      }
    })
  },
  //选择省份
  provinceitem(e) {
    var val = e.currentTarget.dataset.dex
    var t = this.data.provincesdex;
    var cityData = this.data.cityData;
    if (val != t) {
      if (val > 0) {
        let ci = val - 1
        let citys = [];
        for (let i = 0; i < cityData[ci].sub.length; i++) {
          citys.push(cityData[ci].sub[i].name)
        }
        this.setData({
          citys,
          provincesdex: val
        })
      } else if (val == 0) {
        
        this.setData({
          citys:this.data.city_arr,
          provincesdex: val
        })
      }
    }
  },
  //城市
  cityitem(e) {
    var val = e.currentTarget.dataset.dex
    var name = e.currentTarget.dataset.name
    let tablist = this.data.tablist
    tablist[0] = name
    this.setData({
      tablist,
      citysdex: val,
      popshow: false,
      tabdex: "",
      page: 0,
      hospitallm: [],
      city:name
    })
    var keyword = this.data.keyword;
    var order = this.data.order;
    this.getallHospital(keyword, name, order)
  },
  hzanl(e) {
    var that = this
    var hid = e.currentTarget.dataset.hid
    var typs = that.data.typs;
    var leixing = that.data.leixing;
    var id = that.data.id;
    var money = that.data.money;
    var name = that.data.name;
    wx.navigateTo({
      url: "/hyb_yl/userLife/pages/faxian/faxian?hid=" + hid + "&typs=" + typs + '&pinyin=' + that.data.pinyin + '&leixing=' + leixing + '&id=' + id + '&money=' + money + '&name=' + name,
    })
  },
  // tab切换
  tabbtn(e) {
    if (e.currentTarget.dataset.dex === this.data.tabdex) {
      this.setData({
        popshow: false,
        tabdex: "",
      })
    } else {
      wx.pageScrollTo({
        scrollTop: 100,
      })
      this.setData({
        popshow: true,
        tabdex: e.currentTarget.dataset.dex,
      })
    }
  },
  // tab选项
  itemchecked(e) {
    let i = this.data.tabdex,
      dex = e.currentTarget.dataset.dex,
      tabs = this.data.tablist;
    var id = e.currentTarget.dataset.id;


    this.setData({
      order: id,
      hospitallm: [],
      page: 0,
      popshow: false,
      tabdex: ""
    })
    var keyword = this.data.keyword;
    var city = this.data.city;
    this.getallHospital(keyword, city, id)
  },
  hidepop() {
    this.setData({
      popshow: false,
      tabdex: ""
    })
  },
  onPageScroll(e) {
    let h = e.scrollTop;
    let needFix = this.data.needFix;
    let bannerHeight = this.data.bannerHeight;
    if (h > 10 + bannerHeight) {
      this.setData({
        needFix: true
      })
    } else if (h < 20) {
      this.setData({
        needFix: false,
      })
    }
    this.setData({
      bannerHeight: h
    })
  },
  preventTouchMove() {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var keyword = that.data.keyword;
    var order = that.data.order;
    var city = that.data.city;
    console.log(city);
    that.getallHospital(keyword, city, order)
  },
  getallHospital(keyword, city, order) {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.hospital',
      data: {
        page: that.data.page,
        pagesize: that.data.pagesize,
        keyword: keyword,
        city: city,
        order: order
      },
      success: function (res) {
        console.log(res)
        var page = that.data.page + 1;
        that.setData({
          page: page,
          hospitallm: that.data.hospitallm.concat(res.data)
        })
      }
    })
  }

})